package my_double_LinkedList;/*
 *@ 代阳敲的专属代码
 **/

public class DoubleLinkedList {
    // 2、无头双向链表实现
    static  class Node{
        int data;
        Node prev;
        Node next;
        public Node(int data){
            this.data = data;
        }

    }
    Node head;
    Node last;
        //头插法
        public void addFirst(int data){
            Node newNode = new Node(data);
            if (head == null && last == null) {

                head = newNode;
                last = newNode;
            } else {
                newNode.next = head;
                head.prev = newNode;
                head = newNode;
            }

        }


        //尾插法
        public void addLast(int data){
            Node newNode = new Node(data);
            if (head == null && last == null) {

                head = newNode;
                last = newNode;
            } else {
                 last.next = newNode;
                 newNode.prev = last;
                 last = newNode;

            }
        }
        //任意位置插入,第一个数据节点为0号下标
        public void addIndex(int index,int data){
            if(index<0 || index>size()) {
                throw new IndexOutOfBoundsException("Index is out of bounds");
            }
            Node newNode = new Node(data);
            if(index==0) {
                addFirst(data);
            }else if(index==size()) {
                addLast(data);
            } else {
                Node cur=index(index);
                cur.prev.next=newNode;
                newNode.prev=cur.prev;
                newNode.next=cur;
                cur.prev=newNode;
            }
        }

    private Node index(int index) {
            Node cur=head;
            while(index>0) {
                index--;
                cur=cur.next;
            }
            return cur;

    }

    //查找是否包含关键字key是否在单链表当中
        public boolean contains(int key){
            Node cur=head;
            while(cur!=null) {
                if(cur.data==key) {
                    return true;
                }
                cur=cur.next;
            }
            return false;
        }
    public Node containsKey(int key){
        Node cur=head;
        while(cur!=null) {
            if(cur.data==key) {
                return cur;
            }
            cur=cur.next;
        }
        return null;
    }
        //删除第一次出现关键字为key的节点
        public void remove(int key){
            Node cur=containsKey(key);
            if(cur==null) {
                return;
            }
            //头节点
            if(cur.prev==null) {
                head=cur.next;
                cur.next=null;
                head.prev=null;
            }else  if(cur.next==null) {
                last=cur.prev;
                last.next=null;
            } else {
                cur.prev.next=cur.next;
                cur.next.prev=cur.prev;
            }


        }
        public void removeAllKey(int key){
            Node cur=head;
            if(cur==null) {
                return;
            }
            while(cur!=null) {
                if(cur.data==key) {
                    if(cur.prev==null) {
                        head=cur.next;
                        cur.next=null;
                        head.prev=null;
                    }else  if(cur.next==null) {
                        last=cur.prev;
                        last.next=null;
                    } else {
                        cur.prev.next=cur.next;
                        cur.next.prev=cur.prev;
                    }
                }
                cur=cur.next;
            }
        }
        //得到单链表的长度
        public int size(){
            int count=0;
            Node cur=head;
            while(cur!=null) {
                count++;
                cur=cur.next;
            }
            return count;
        }


        public void display(){
            Node cur=head;
            while(cur!=null) {
                System.out.print(cur.data+" ");
                cur=cur.next;
            }
            System.out.println();
        }
        public void clear(){}
    }

